<?php
/**
 * Created by JetBrains PhpStorm.
 * User: PixoiD
 * Date: 09.09.11
 * Time: 10:39
 * To change this template use File | Settings | File Templates.
 */
class FormNewSuggest extends Suggest {

    public $config = array();
    public $path;
    public $tags;

    public function rules() {
        return array(
            array('title', 'length', 'max' => 255, 'min' => 3),
            array('title, text, tags', 'required'),
            array('tags', 'tagsLimit'),
            array('files', 'file', 'types' => 'jpg, gif, png, doc, docx, xls, xlsx', 'maxSize' => 1024 * 1024 * 100, 'allowEmpty' => true),
            array('owner_id, send_email', 'safe'),
        );
    }

    public function beforeValidate() {
        $this->title = trim(strip_tags($this->title));
        return parent::beforeValidate();
    }

    public function init() {
        $this->config = array(
            'class' => 'form',
            'enctype' => 'multipart/form-data',
            'elements' => array(
                'title' => array('type' => 'text'),
                'text' => array('type' => 'ext.redactor.EImperaviRedactorWidget', 'options' => array('toolbar' => 'custom')),
                'files' => array('type' => 'file'),
                'tags' => array('type' => 'ext.multicomplete.MultiComplete', 'splitter' => ',', 'sourceUrl' => '/user/autocomplete', 'options' => array('minLength' => '2')),
                'send_email' => array('type' => 'checkbox', 'layout' => "<br/>{input} {label}\n{hint}\n{error}"),
            ),
            'buttons' => array(
                'send' => array('type' => 'htmlSubmit', 'value' => 1, 'class' => 'blue', 'label' => '<span class="label">Добавить предложение</span>'),
            )
        );
        return parent::init();
    }

    public function  tagsLimit($object, $attribute) {
        $tags = explode(",", $this->tags);
        $tags = array_unique($tags);
        $this->tags = join(", ", $tags);
        if(count($tags) > 3) {
            $this->addError('tags', 'Что-то много тегов, максимум 3.');
        } else
            return;
    }

    public function createNew() {
        if(!$this->hasErrors()) {
            $this->owner_id = Yii::app()->user->id;
            //tags
            $ids = Tags::addNewTags($this->tags);
            //file
            $this->files = CUploadedFile::getInstance($this, 'files');
            if($this->files) {
                $path = Yii::app()->basePath . '/../upload/suggest/';
                if(!file_exists($path)) {
                    @mkdir($path, 0777);
                    @chmod($path, 0777);
                }
                $path = Yii::app()->basePath . '/../upload/suggest/' . Yii::app()->user->id . "/";
                $path_to_base = '/upload/suggest/' . Yii::app()->user->id . "/";
                $unique_name = substr(md5(time()), 0, 8);
                $ext = $this->files->getExtensionName();
                $file_to_base = $path_to_base . $unique_name . '.' . $ext;
                $file = $path . $unique_name . '.' . $ext;
                if(!file_exists($path)) {
                    @mkdir($path, 0777);
                    @chmod($path, 0777);
                }
                $this->files->saveAs($file);
                $this->files = $file_to_base;
            }
            $this->save(false);
            TagsSuggest::links($this->id, $ids);
        }
    }

}
